#include "mainwindow.h"
#include "ui_mainwindow.h"



void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Return :
        handleSongSearch();
        return;
    case Qt::Key_Space:
        handlePlaySlot();
        return;
    //快进
    case Qt::Key_Right:
        music_player->setPosition(music_player->position()+5*1000);
        return;
    //后退
    case Qt::Key_Left:
        music_player->setPosition(music_player->position()-5*1000);
        return;
    }


    QMainWindow::keyPressEvent(event);  // 其他按键交给父类处理
}

bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
    //条件判断
    if ((watched==ui->music_list->viewport()
         || watched==ui->download_list->viewport()
         ||watched==custom_song_list->viewport()
         ||watched==ui->song_data->viewport())
        &&event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->button() == Qt::LeftButton || mouseEvent->button() == Qt::RightButton)
        {
            // 拦截事件，阻止选择发生
            return true;
        }
    }
    //鼠标点击调整歌曲进度
    if (watched==ui->music_slider && event->type() == QEvent::MouseButtonRelease)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        //获取鼠标位置
        QPoint globalPos = mouseEvent->globalPosition().toPoint();
        //映射到组件中
        QPoint SliderPos = ui->music_slider->mapFromGlobal(globalPos);
        //
        music_player->setPosition(SliderPos.x()*music_player->duration()/ui->music_slider->width());
        SilderRelease(music_player,ui->music_slider);
    }
    if (event->type() == QEvent::MouseButtonPress && custom_song_list->isVisible())
    {
        // 获取鼠标点击的全局位置
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        QPoint globalPos = mouseEvent->globalPosition().toPoint();
        // 将全局位置转换为相对 tableView 的坐标
        QPoint tableViewPos = custom_song_list->mapFromGlobal(globalPos);

        //布局的区域

        //qDebug()<<"esfdsefwsefsf";
        //点击待待播放歌曲列表之外的地方就隐藏该列表(下一首上一首等组件除外)
        if (!custom_song_list->rect().contains(tableViewPos)
            &&!ui->btn_next->rect().contains(ui->btn_next->mapFromGlobal(globalPos))
            &&!ui->btn_pre->rect().contains(ui->btn_pre->mapFromGlobal(globalPos))
            &&!ui->btn_play->rect().contains(ui->btn_play->mapFromGlobal(globalPos))
            &&!ui->music_slider->rect().contains(ui->music_slider->mapFromGlobal(globalPos))
            )
        {
            custom_song_list->hide();
            return true; // 事件已处理
        }
    }
    //音量控制界面
    if (event->type() == QEvent::MouseButtonPress && ui->sound_widget->isVisible())
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        QPoint globalPos = mouseEvent->globalPosition().toPoint();
        QPoint SoundWidgetPos= ui->sound_widget->mapFromGlobal(globalPos);
        if (!ui->sound_widget->rect().contains(SoundWidgetPos))
        {
            ui->sound_widget->hide();
            return true; // 事件已处理
        }
    }
    return QMainWindow::eventFilter(watched, event); // 其他事件继续传递
}
